package com.magicalcoder.pigeontest.server.zookeeper;

import com.magicalcoder.pigeon.common.callback.NettyStartSuccessCallback;
import com.magicalcoder.pigeon.server.PigeonNettyServer;
import com.magicalcoder.pigeon.zookeeper.RegisterServer;
import com.magicalcoder.pigeontest.service.HelloPersonService;
import com.magicalcoder.pigeontest.service.HelloService;
import com.magicalcoder.pigeontest.service.impl.HelloPersonServiceImpl;
import com.magicalcoder.pigeontest.service.impl.HelloServiceImpl;
import org.apache.zookeeper.KeeperException;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by hzhedongyu on 2017/5/27.
 * 必须有zookeeper做支持
 * host:port/serviceName需要显示注册到zookeeper
 */
public class ServerStart {
    public static void main(String[] args) throws InterruptedException {
        HelloPersonService helloPersonService = new HelloPersonServiceImpl();
        HelloService helloService = new HelloServiceImpl();
        Map<String,Object> kv = new HashMap<>();
        kv.put(HelloPersonService.class.getName(),helloPersonService);
        kv.put(HelloService.class.getName(),helloService);

        String host = "localhost";
        int port = 8881;
        String serviceName = "userService";

        PigeonNettyServer server = new PigeonNettyServer(kv);
        server.start(port, new NettyStartSuccessCallback() {
            @Override
            public void call() {
                RegisterServer registerServer = new RegisterServer();
                try {
                    registerServer.bind(host, port, serviceName);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }
        });
    }
}